//
//  SkToast.m
//  YOYOLive
//
//  Created by 王根强 on 2016/10/28.
//  Copyright © 2016年 王根强. All rights reserved.
//

#import "YOToast.h"
#define SKmScreenWidth          ([UIScreen mainScreen].bounds.size.width)
#define SKmScreenHeight         ([UIScreen mainScreen].bounds.size.height)

@implementation YOToast;
+ (void)YOToastShow:(NSString *)title withHight:(CGFloat) hight{
    Toast *toast=[Toast shared];
    [toast initToastView:title withHight:hight];
}

+ (void)YOToastShow:(NSString *)title{
    [self YOToastShow:title withHight:0];
}

@end


@implementation Toast

+ (instancetype)shared {
    static id __staticObject = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __staticObject = [Toast new];
    });
    return __staticObject;
}


- (instancetype)init {
    if (self = [super init]) {
        _labToast=[[UILabel alloc] init];
    }
    return self;
}

- (void)initToastView:(NSString *)title withHight:(CGFloat)hight {
    if (!_bShow) {
        _bShow=YES;
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, -64, kGScreenWidth, 64)];
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
        [_bgView addGestureRecognizer:panGesture];
        _bgView.backgroundColor = RGB_VALUE(0x207dd4);
        
        _labToast.frame = CGRectMake(0, 30, kGScreenWidth, 20);
        _labToast.textAlignment = NSTextAlignmentCenter;
        _labToast.text = title;
        _labToast.font = kFont14;
        _labToast.backgroundColor = [UIColor clearColor];
        _labToast.textColor = [UIColor whiteColor];
        [_bgView addSubview:_labToast];

        [UIView animateWithDuration:0.2 animations:^{
            _bgView.frame = CGRectMake(0, 0, kGScreenWidth, 64);
        }];
        [window addSubview:_bgView];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.2 animations:^{
                _bgView.frame = CGRectMake(0, -64, kGScreenWidth, 64);
            }];
            _bShow = NO;
        });
    }
}

//拖动手势触发方法
-(void) panGesture:(id)sender {
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.frame = CGRectMake(0, -64, kGScreenWidth, 64);
    }];
}

@end
